package actionbean;

import java.util.ArrayList;
import java.util.Collections;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import util.JsonUtil;
import controller.TimetableCtrl;
import controller.comparator.CourseComparatorByExamStart;
import entity.Course;
import entity.Section;

@UrlBinding(BaseActionBean.URI_FOR_TIMETABLE
		+ "{$event}/{courseCode}/{sectionCode}")
public class TimetableActionBean extends BaseBidActionBean {

	private TimetableCtrl timetableCtrl = new TimetableCtrl();

	@HandlesEvent("exam")
	public Resolution viewExamTimetable() {
		return new ForwardResolution(VIEW_FOR_EXAM_TIMETABLE);
	}

	@DefaultHandler
	public Resolution view() {
		loadData();
		return new ForwardResolution(VIEW_FOR_TIMETABLE);
	}

	public String getEventsJson() {
		return JsonUtil.escapeJavascriptDate(timetableCtrl
				.retrieveEventsInTimetableForUserId(getLoggedInUserId()), 4);
	}

	public ArrayList<Section> getEventSections() {
		return timetableCtrl
				.retrieveSectionsInTimetableForUserId(getLoggedInUserId());
	}

	public ArrayList<Course> getEventCourses() {
		return timetableCtrl
				.retrieveCoursesInTimetableForUserId(getLoggedInUserId());
	}

	public ArrayList<Course> retrieveSuccessfulCourses() {
		ArrayList<Course> courses = timetableCtrl
				.retrieveSuccessfulCourses(getLoggedInUserId());
		Collections.sort(courses, new CourseComparatorByExamStart());
		return courses;
	}

	public ArrayList<Course> retrievePendingCourses() {
		ArrayList<Course> courses = timetableCtrl
				.retrievePendingCourses(getLoggedInUserId());
		Collections.sort(courses, new CourseComparatorByExamStart());
		return courses;
	}

	@Override
	protected String redirectUriAfterSuccessfulBidOperation() {
		return URI_FOR_TIMETABLE;
	}

}
